<?php
defined('SEJUJOPEFDNGJSL') or exit('Access denied');
//记录日志，第一个参数是内容
function record_log($c,$t=1){
	global $db,$_username;
	$url=basename($_SERVER['PHP_SELF']); //文件的名字小写当前正在执行脚本的文件名

	if(!empty($_SERVER["QUERY_STRING"])) $url.="?".$_SERVER["QUERY_STRING"]; //$_SERVER["QUERY_STRING"]  获取查询 语句，实例中可知，获取的是?后面的值
	$db->sitedbexec("insert into ld_log(uname,login_time,log_type,content,url) values('".$_username."',".time().",$t,'$c','$url')");
}
//读取管理后台配置
function getconfig(){
	global $db;
	$db->query("select * from ld_settings");
	// var_dump($db->rs);
	// exit();
	foreach ($db->rs as $k => $v) {
		if(strstr($v['setting_value'],'|')){      //查找 | 第一次出现的位置 并且 返回剩余的字符串
			$arr=explode('|',$v['setting_value']);
			$GLOBALS['GLOBALARR'][$v['setting_enname']]=$arr;
		}else{
			$GLOBALS['GLOBALARR'][$v['setting_enname']]=$v['setting_value'];
		}
	}
}

//手动转收益
function chkzhuanshouyi($arr){
	global $GLOBALARR;
	//收益次数大于利息收益手动转换（天）（目前是0）并且提供帮助金额等于提供帮助者确认金额并且已上传凭证并且未获取收益
	if($arr["amount"]==$arr["helper_confirm_amount"]&&$arr['giveh_eviup_time']>0&$arr['had_shouyi']==0){
            // $cha=(time()-$arr['time'])/(24*3600);
            // if($cha>=$GLOBALARR['pdday']){
            	return true;
            // }
	}
	return false;
}

//提供帮助倒计时，过期未上传打款证明的冻结账户（finished0518）
function chkcunkdaojishi(){

	global $db,$GLOBALARR,$member;
	try{  //
		$db->begintransaction();   //开启事务处理
		//查找配对交易时间到当前时间的时间差-大于系统设定的-提供帮助和获得帮助确认期限-并且过期标记是0-并且结束标记是0-并且没有上传提供帮助证明的提供帮助表id（配对表）
		$db->query("select give_help_id from ld_remit_list where  is_overdate=0 and TIMESTAMPDIFF(minute , from_unixtime(deal_time,'%Y-%m-%d %H:%i:%S'),  '".date("Y-m-d H:i:s")."')>=".$GLOBALARR["jjqkok"]." and is_finnished=0 and evidence_upload_time is null");
		$rs=$db->rs;
		foreach ($rs as $k => $v) {
			//选取过期标记是0结束标记是0的记录（配对表）
			$db->lspre("select give_help_id,get_help_id,id,amount from ld_remit_list where is_overdate=0 and is_finnished = 0 and give_help_id=:t0",1,array(array(':t0'),array($v["give_help_id"])));
			$rss=$db->rs;
			foreach ($rss as $key => $value) {   //	查找超时的账号封号
				//更新配对信息为已作废，已经确认收款的除外
				$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='gve打款超时' where is_overdate=0 and is_finnished = 0 and id=:t0",4,array(array(':t0'),array($value["id"])));
				//获取提供帮助信息，选取提供帮助金额不等于提供帮助者已确认金额并且状态是等待配对或是已完成的记录（提供帮助表）
				$db->lspre("select * from ld_give_help where amount!=helper_confirm_amount and `status`<2 and id=:t0",2,array(array(':t0'),array($value["give_help_id"])));
				$gps=$db->rs;
				if(!empty($gps)){
					//冻结提供帮助人
					//用户账户表更新冻结状态 冻结原因 以及冻结时间
					$db->lspre("update ld_user_account set is_freezed=1,freezed_time=:t2,freezed_reason=:t1 where id=:t0",4,array(array(':t0',':t1',':t2'),array($gps["uid"],'提供帮助未及时上传汇款证明',time())));
					//锁定提供帮助人的所有提供帮助
					$db->lspre("update ld_give_help set give_help_freeze=1,give_help_zfreason='gve打款超时' where status<2 and uid=:t0",4,array(array(':t0'),array($gps["uid"])));
					//锁定提供帮助人的所有获得帮助
					$db->lspre("update ld_get_help set freeze_mark=1,zfreason='gve打款超时' where status<2 and get_help_uid=:t0",4,array(array(':t0'),array($gps["uid"])));
					//作废提供帮助人的获得帮助单
					$db->query("select * from ld_get_help where get_help_amount!=receiver_confirm_amount and `status`<2 and get_help_uid=".$gps["uid"]);
					$user_get_help = $db->rs;
					if(!empty($user_get_help)){
						foreach($user_get_help as $kkk=>$vvv){
							$db->query("select * from ld_remit_list where is_overdate=0 and is_finnished = 0 and get_help_id=".$vvv['id']);
							$wait_overdate_get = $db->rs;
							//作废未完成的汇款记录
							$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='gve打款超时' where is_overdate=0 and is_finnished = 0 and get_help_id=:t0",4,array(array(':t0'),array($vvv["id"])));
							foreach($wait_overdate_get as $val){
								//更新提供帮助表，记录获得帮助人未及时确认收款
								$db->lspre("update ld_give_help set get_helper_unconfirm=1 where id=:t0",4,array(array(':t0'),array($val["give_help_id"])));
							}
						}
					}
					$give_helper_info=$member->getuv1($gps["uid"]);
					if(!empty($give_helper_info['introducer_id'])){
						$leader_info=$member->getuv1($give_helper_info['introducer_id']);
						if($leader_info['cash_ldprize']>0){
							writeaccount(array($leader_info['id'],"1",9,time(),20,($leader_info['cash_ldprize']*0.1),$give_helper_info['uname'].'提供帮助打款超时扣上级10%的领导奖'));
						}
					}
				}
				//更新获得帮助，获得帮助已配对金额回退，继续寻找配对
				$db->lspre("update ld_get_help set matched_amount=matched_amount-:t1,`status`=0 where id=:t0",4,array(array(':t0',':t1'),array($value["get_help_id"],$value["amount"])));
			}//超时封号所有的数据更新
		}
		$db->committransaction();  //所有的SQL都执行成功后就开启自动提交
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
}

//获得帮助倒计时，查找提供帮助者上传完提供帮助证明后获得帮助者没有及时确认收款的记录，冻结获得帮助者的帐号（finished0517）
function chkqukdaojishi(){
	global $db,$GLOBALARR,$member;
	try{
		$db->begintransaction();     //事物的开始
		//从配对表中查找-提供帮助者上传提供帮助证明时间-到当前时间大于系统设置的-获得帮助确认期限-并且过期标记是0并且结束标记是0并且-获得帮助者没有确认已获得帮助-的记录（配对表）
		$db->query("select get_help_id from ld_remit_list where is_overdate=0 and is_finnished=0 and remittee_confirm_time is null and evidence_upload_time is not null and TIMESTAMPDIFF(minute , from_unixtime(evidence_upload_time,'%Y-%m-%d %H:%i:%S'),  '".date("Y-m-d H:i:s")."')>=".$GLOBALARR["jjtx"]);
		$rs=$db->rs;
		foreach ($rs as $k => $v) {
			//选取过期标记是0，结束标记是0的记录（配对表）
			$db->lspre("select * from ld_remit_list where is_overdate=0 and is_finnished=0 and get_help_id=:t0",1,array(array(':t0'),array($v["get_help_id"])));
			$rss=$db->rs;
			foreach ($rss as $key => $value) {
				//查询获得帮助表，查找获得帮助金额不等于已确认金额并且状态不是已过期的记录
				$db->lspre("select * from ld_get_help where get_help_amount!=receiver_confirm_amount and `status`<2 and id=:t0",2,array(array(':t0'),array($value["get_help_id"])));
				$gps=$db->rs;
				if(!empty($gps)){
					//冻结获得帮助人
					$db->lspre("update ld_user_account set is_freezed=1,freezed_time=:t1,freezed_reason='获得帮助人未及时确认' where id=:t0",4,array(array(':t0',':t1'),array($gps["get_help_uid"],time())));
					//锁定获得帮助人的所有提供帮助
					$db->lspre("update ld_give_help set give_help_freeze=1,give_help_zfreason='get超时确认收款' where `status`<2 and uid=:t0",4,array(array(':t0'),array($gps["get_help_uid"])));
					//锁定获得帮助人的所有获得帮助
					$db->lspre("update ld_get_help set freeze_mark=1,zfreason='get超时确认收款' where `status`<2 and get_help_uid=:t0",4,array(array(':t0'),array($gps["get_help_uid"])));
					//作废获得帮助人的提供帮助订单
					$db->query("select * from ld_give_help where amount!=helper_confirm_amount and `status`<2 and uid=".$gps["get_help_uid"]);
					$user_give_help = $db->rs;
					//作废未完成的提供帮助汇款记录
					if(!empty($user_give_help)){
						foreach($user_give_help as $kkk=>$vvv){
							$db->query("select * from ld_remit_list where is_overdate=0 and is_finnished = 0 and give_help_id=".$vvv['id']);
							$user_overdate_give = $db->rs;
							$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='get超时确认收款' where is_overdate=0 and is_finnished = 0 and give_help_id=:t0",4,array(array(':t0'),array($vvv["id"])));
							foreach($user_overdate_give as $uog){
								//更新获得帮助，获得帮助已配对金额回退，继续寻找配对
								$db->lspre("update ld_get_help set matched_amount=matched_amount-:t1,`status`=0 where id=:t0",4,array(array(':t0',':t1'),array($uog["get_help_id"],$uog["amount"])));
							}
						}
					}
				}
				//更新配对信息已过期作废
				$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='get超时确认收款' where is_overdate=0 and is_finnished = 0 and get_help_id=:t0",4,array(array(':t0'),array($value["get_help_id"])));
				//更新提供帮助表，记录获得帮助人未及时确认收款
				$db->lspre("update ld_give_help set get_helper_unconfirm=1 where id=:t0",4,array(array(':t0'),array($value["give_help_id"])));
			}
		}
		$db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
}

function get_give_num($uid){
	global $db;
	$bgtime=strtotime(date("Y-m-d H:i:s",strtotime("-30 day")));
	$now=time();
	$db->query("select count(*) as num from ld_give_help where status<3 and give_help_time between '".$bgtime."' and '".$now."' and uid='".$uid."'",2);
	$num=$db->rs["num"];
	return $num;
}
//提供帮助完成一单后需要在48小时内挂下一单，超时封号
function chkqucun(){
	global $db,$GLOBALARR,$_userid,$username,$member;
	if(empty($_userid) || $_userid == 8501){    //如果是管理员或者不存在 则返回
		return;
	}
	$db->query("select * from ld_give_help where giveh_eviup_time>0 and uid = ".$_userid." and helper_confirm_amount=amount order by giveh_eviup_time desc limit 0,1",2);
	$last_give_help = $db->rs;
	if(!empty($last_give_help)){
		$last_finished_time = $last_give_help['giveh_eviup_time'];
	}else{
		return;
	}
	//查询该用户是否复投
	$isft=0;
	$db->query("select * from ld_give_help where status<2 and uid = ".$_userid." and giveh_eviup_time is null and helper_confirm_amount!=amount order by give_help_time desc limit 0,1",2);
	$last_ft = $db->rs;
	if(!empty($last_ft) && $last_ft['give_help_time'] > $last_finished_time){
		$isft = 1;
	}
	//如果已经完成过一单并且当前时间到最近一次完成时间超过24小时则封号
	if($last_finished_time > 0 && time()-$last_finished_time>$GLOBALARR['jjqc']*60 && $isft==0){
		try{
			$db->begintransaction();
			//冻结用户
			$db->lspre("update ld_user_account set is_freezed=1,freezed_time=:t2,freezed_reason=:t1 where id=:t0",4,array(array(':t0',':t1',':t2'),array($_userid,'提供帮助完成8天后没有再挂单',time())));
			$db->query("select * from ld_give_help where amount!=helper_confirm_amount and `status`<2 and uid=".$_userid);
			$user_give_help = $db->rs;
			//作废未完成的提供帮助汇款记录
			if(!empty($user_give_help)){
				foreach($user_give_help as $k=>$v){
					$db->query("select * from ld_remit_list where is_overdate=0 and is_finnished = 0 and give_help_id=".$v['id']);
					$user_overdate_give = $db->rs;
					$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='提供帮助完成8天后没有再挂单' where is_overdate=0 and is_finnished = 0 and give_help_id=:t0",4,array(array(':t0'),array($v["id"])));
					foreach($user_overdate_give as $uog){
						//更新获得帮助，获得帮助已配对金额回退，继续寻找配对
						$db->lspre("update ld_get_help set matched_amount=matched_amount-:t1,`status`=0 where id=:t0",4,array(array(':t0',':t1'),array($uog["get_help_id"],$uog["amount"])));
					}
				}
			}
			//作废未完成的获得帮助汇款记录
			$db->query("select * from ld_get_help where get_help_amount!=receiver_confirm_amount and `status`<2 and get_help_uid=".$_userid);
			$user_get_help = $db->rs;
			if(!empty($user_get_help)){
				foreach($user_get_help as $key=>$val){
					$db->query("select * from ld_remit_list where is_overdate=0 and is_finnished = 0 and get_help_id=".$val['id']);
					$user_overdate_get = $db->rs;
					//作废未完成的汇款记录
					$db->lspre("update ld_remit_list set is_overdate=1,overdate_reason='提供帮助完成8天后没有再挂单' where is_overdate=0 and is_finnished = 0 and get_help_id=:t0",4,array(array(':t0'),array($val["id"])));
					foreach($user_overdate_get as $value){
						//更新提供帮助表，记录获得帮助人未及时确认收款
						$db->lspre("update ld_give_help set get_helper_unconfirm=1 where id=:t0",4,array(array(':t0'),array($value["give_help_id"])));
					}
				}
			}
			//锁定用户的所有提供帮助
			$db->lspre("update ld_give_help set give_help_freeze=1,give_help_zfreason='提供帮助完成8天后没有再挂单' where status<2 and uid=:t0",4,array(array(':t0'),array($_userid)));
			//锁定用户的所有获得帮助
			$db->lspre("update ld_get_help set freeze_mark=1,zfreason='提供帮助完成8天后没有再挂单' where status<2 and get_help_uid=:t0",4,array(array(':t0'),array($_userid)));
			$db->committransaction();
		}catch(PDOException $e){
			$db->rollbacktransaction();
			a_bck("error");
		}
	}
}

function getlevel($v,$t=0){
	global $GLOBALARR;
	//级别名称
	if(is_array($GLOBALARR["jjname"])){
		if($t){
			//级别开通所需激活币金额---yili
			return $GLOBALARR["jjrmb"][$v];
		}else{
			return $GLOBALARR["jjname"][$v];
		}
	}else{
		if($t){
			return $GLOBALARR["jjrmb"];
		}else{
			return $GLOBALARR["jjname"];
		}
	}
}
	//获取推荐人
function getparentuser($s){
		global $db;
		if(empty($s)){
			return false;
		}
		$db->query("select uname from ld_user_account where id = $s ",2);
		$aa = $db->rs;
		if(empty($aa)){
			return false;
		}else{
			return $aa['uname'];
		}

	}
function auto_recomm($name){
	global $db;
	$db->query("select id from ld_user_account where  uname='".$name."'",2);
	$uid=$db->rs['id'];
	if(!$uid){
		return;
	}
	$db->query("select count(id) as num from ld_user_account where introducer_id=".$uid,2);
	$num=$db->rs['num'];
	if(!$num){
		$num=0;
	}

	$db->query("update ld_user_account set recommend_unum=".$num." where id=".$uid,4);

}
function getgive($s){
	global $db;
	$db->query("select * from ld_give_help where id = $s",2);
	$aa = $db->rs;
	if(empty($aa)){
		return false;
	}else{
		return $aa;
	}
}
function getnews(){
	global $db;
	$db->query('select * from ld_article where is_show=0 and is_on_top=1 order by is_on_top desc,art_time desc limit 1',2);
	$rn=$db->rs;

	$db->query('select * from ld_article where is_show=0 order by is_on_top desc,art_time desc limit 5');
	$rns=$db->rs;
	return array($rn,$rns);
}

function teditinfo($k,$v,&$arr){

	switch ($k) {
		case 'rgmbq':
			$arr['mbq']=$v;
		break;
		case 'rgmba':
			$arr['rgmba']=$v;
		break;
		case 'rgmobile':
			$arr['phone']=$v;
		break;
		case 'rgemail':
			$arr['rgemail']=$v;
		break;
		case 'rgcard':
			$arr['rgcard']=$v;
		break;
		case 'rgaddress':
			$arr['rgaddress']=$v;
		break;
		case 'rgweixin':
			$arr['rgweixin']=$v;
		break;
		case 'rgqq':
			$arr['rgqq']=$v;
		break;
		case 'rgsex':
			$arr['sex']=$v;
		break;
		case 'rgfax':
			$arr['rgfax']=$v;
		break;
		case 'province':
			$arr['province']=$v;
		break;
		case 'city':
			$arr['city']=$v;
		break;
		case 'area':
			$arr['area']=$v;
		break;
		case 'real_name':
			$arr['real_name']=$v;
		break;
	}

}

//更新用户表，向提供帮助记录表插入数据
function writeaccount($arr){
	global $db,$GLOBALARR,$member;
	$us=$member->getuvinfo($arr[0],"*");
	$jin=$arr[5];
	// var_dump($jin);
	// return;
	if($arr[1]==0){
		if($arr[2]==0){
			//增加可提现余额
			$sql="update ld_user_account set cash_purse=cash_purse+:t1 where id=:t0";
			$yu=$us["cash_purse"]+$jin;
		}elseif($arr[2]==1){
			//增加收益钱包
			$sql="update ld_user_account set benifit_purse=benifit_purse+:t1 where id=:t0";    //benifit_purse历史投资总金额
			$yu=$us["benifit_purse"]+$jin;
		}elseif($arr[2]==2){
			//增加佣金钱包
			$sql="update ld_user_account set commission_purse=commission_purse+:t1 where id=:t0";
			$yu=$us["commission_purse"]+$jin;
		}elseif($arr[2]==3){
			//增加激活币
			$sql="update ld_user_account set active_coin=active_coin+:t1 where id=:t0";
			$yu=$us["active_coin"]+$jin;
		}elseif($arr[2]==9){
			//增加可提现领导奖
			$sql="update ld_user_account set cash_ldprize=cash_ldprize+:t1 where id=:t0";
			$yu=$us["cash_ldprize"]+$jin;
		}elseif($arr[2]==8){
			//增加排单币
			$sql="update ld_user_account set give_help_coin=give_help_coin+:t1 where id=:t0";
			$yu=$us["give_help_coin"]+$jin;
		}elseif($arr[2]==11){
			//增加利息
			$sql="update ld_user_account set lixi_crash=lixi_crash+:t1 where id=:t0";
			$yu=$us["lixi_crash"]+$jin;
		}elseif($arr[2]==12){
			//增加商城积分
			$sql="update ld_user_account set point_crash=point_crash+:t1 where id=:t0";
			$yu=$us["point_crash"]+$jin;
		}elseif($arr[2]==13){
			//增加积分
			$sql="update ld_user_account set rise_crash=rise_crash+:t1 where id=:t0";
			$yu=$us["rise_crash"]+$jin;
		}elseif($arr[2]==14){
			//增加1代冻结领导奖
			$sql="update ld_user_account set fir_freezedldj=fir_freezedldj+:t1 where id=:t0";
			$yu=$us["fir_freezedldj"]+$jin;
		}
		elseif($arr[2]==15){
			//增加2代动态领导奖
			$sql="update ld_user_account set sec_freezedldj=sec_freezedldj+:t1 where id=:t0";
			$yu=$us["sec_freezedldj"]+$jin;
		}
		elseif($arr[2]==16){
			//增加4代动态领导奖
			$sql="update ld_user_account set four_freezedldj=four_freezedldj+:t1 where id=:t0";
			$yu=$us["four_freezedldj"]+$jin;
		}
		elseif($arr[2]==17){
			//增加冻结利息
			$sql="update ld_user_account set freeze_lixi=freeze_lixi+:t1 where id=:t0";
			$yu=$us["freeze_lixi"]+$jin;
		}
		elseif($arr[2]==18){
			//增加冻结动态
			$sql="update ld_user_account set freeze_ldj=freeze_ldj+:t1 where id=:t0";
			$yu=$us["freeze_ldj"]+$jin;
		}

		elseif($arr[2]==20){
			//增加共享积分
			$sql="update ld_user_account set fuxiao_crash=fuxiao_crash+:t1 where id=:t0";
			$yu=$us["fuxiao_crash"]+$jin;
		}elseif($arr[2]==21){
			//增加共享积分
			$sql="update ld_user_account set fuxiao_jin=fuxiao_jin+:t1 where id=:t0";
			$yu=$us["fuxiao_jin"]+$jin;
		}
		elseif($arr[2]==22){
			//增加共享积分
			$sql="update ld_user_account set fazhan_crash=fazhan_crash+:t1 where id=:t0";
			$yu=$us["fazhan_crash"]+$jin;
		}elseif($arr[2]==23){
			//增加冻结本金
			$sql="update ld_user_account set freeze_benjin=freeze_benjin+:t1 where id=:t0";
			$yu=$us["freeze_benjin"]+$jin;
		}elseif($arr[2]==25){
			//增加冻结本金
			$sql="update ld_user_account set freeze_benjin=freeze_benjin+:t1 where id=:t0";
			$yu=$us["freeze_benjin"]+$jin;
		}elseif($arr[2]==156){
			//增加冻结本金
			$sql="update ld_user_account set btc_account=btc_account+:t1 where id=:t0";
			$yu=$us["btc_account"]+$jin;
		}elseif($arr[2]==157){
			//增加冻结本金
			$sql="update ld_user_account set ltc_account=ltc_account+:t1 where id=:t0";
			$yu=$us["ltc_account"]+$jin;
		}
		$db->lspre($sql,4,array(array(':t0',':t1'),array($us["id"],$jin)));  //执行sql语句

		//向提供帮助记录表里添加记录
		$sql="insert into ld_help_record(uid,uname,help_record_time,get_envelope_type,plus_amount,usable_amount,operation_type,description) values(:t1,:t2,:t3,:t4,:t5,:t6,:t7,:t8)";
		$db->lspre($sql,4,array(array(':t1',':t2',':t3',':t4',':t5',':t6',':t7',':t8'),array($us["id"],$us["uname"],$arr[3],$arr[4],$jin,$yu,$arr[2],$arr[6])));
	}else{
		//这些是对应上面的操作，减
		if($arr[2]==0){
			$sql="update ld_user_account set cash_purse=cash_purse-:t1 where id=:t0";
			$sqll="update ld_user_account set cash_purse=0 where id=:t0 and cash_purse<0";
			$yu=$us["cash_purse"]-$jin;
		}elseif($arr[2]==1){
			$sql="update ld_user_account set benifit_purse=benifit_purse-:t1 where id=:t0";
			$sqll="update ld_user_account set benifit_purse=0 where id=:t0 and benifit_purse<0";
			$yu=$us["benifit_purse"]-$jin;
		}elseif($arr[2]==2){
			$sql="update ld_user_account set commission_purse=commission_purse-:t1 where id=:t0";
			$sqll="update ld_user_account set commission_purse=0 where id=:t0 and commission_purse<0";
			$yu=$us["commission_purse"]-$jin;
		}elseif($arr[2]==3){
			$sql="update ld_user_account set active_coin=active_coin-:t1 where id=:t0";
			$sqll="update ld_user_account set active_coin=0 where id=:t0 and active_coin<0";
			$yu=$us["active_coin"]-$jin;
		}elseif($arr[2]==9){
			$sql="update ld_user_account set cash_ldprize=cash_ldprize-:t1 where id=:t0";
			$sqll="update ld_user_account set cash_ldprize=0 where id=:t0 and cash_ldprize<0";
			$yu=$us["cash_ldprize"]-$jin;
		}elseif($arr[2]==8){    //减拍单币
			$sql="update ld_user_account set give_help_coin=give_help_coin-:t1 where id=:t0";
			$sqll="update ld_user_account set give_help_coin=0 where id=:t0 and give_help_coin<0";
			$yu=$us["give_help_coin"]-$jin;   //剩余的拍单币
		}elseif($arr[2]==11){    //减利息
			$sql="update ld_user_account set lixi_crash=lixi_crash-:t1 where id=:t0";
			$sqll="update ld_user_account set lixi_crash=0 where id=:t0 and lixi_crash<0";
			$yu=$us["lixi_crash"]-$jin;
		}elseif($arr[2]==12){    //减积分
			$sql="update ld_user_account set point_crash=point_crash-:t1 where id=:t0";
			$sqll="update ld_user_account set point_crash=0 where id=:t0 and point_crash<0";
			$yu=$us["point_crash"]-$jin;   //剩余的拍单币
		}elseif($arr[2]==13){    //减晋级奖
			$sql="update ld_user_account set rise_crash=rise_crash-:t1 where id=:t0";
			$sqll="update ld_user_account set rise_crash=0 where id=:t0 and rise_crash<0";
			$yu=$us["rise_crash"]-$jin;   //剩余的拍单币
		}elseif($arr[2]==17){    //减
			$sql="update ld_user_account set freeze_lixi=freeze_lixi-:t1 where id=:t0";
			$sqll="update ld_user_account set freeze_lixi=0 where id=:t0 and freeze_lixi<0";
			$yu=$us["freeze_lixi"]-$jin;   //剩余的拍单币
		}elseif($arr[2]==18){    //减动态积分
			$sql="update ld_user_account set freeze_ldj=freeze_ldj-:t1 where id=:t0";
			$sqll="update ld_user_account set freeze_ldj=0 where id=:t0 and freeze_ldj<0";
			$yu=$us["freeze_ldj"]-$jin;   //剩余的拍单币
		}elseif($arr[2]==20){    //减赠送积分
			$sql="update ld_user_account set fuxiao_crash=fuxiao_crash-:t1 where id=:t0";
			$sqll="update ld_user_account set fuxiao_crash=0 where id=:t0 and fuxiao_crash<0";
			$yu=$us["fuxiao_crash"]-$jin;
		}elseif($arr[2]==21){    //减共享积分
			$sql="update ld_user_account set fuxiao_jin=fuxiao_jin-:t1 where id=:t0";
			$sqll="update ld_user_account set fuxiao_jin=0 where id=:t0 and fuxiao_jin<0";
			$yu=$us["fuxiao_jin"]-$jin;
		}elseif($arr[2]==22){    //减共享积分
			$sql="update ld_user_account set fazhan_crash=fazhan_crash-:t1 where id=:t0";
			$sqll="update ld_user_account set fazhan_crash=0 where id=:t0 and fazhan_crash<0";
			$yu=$us["fazhan_crash"]-$jin;
		}elseif($arr[2]==23){    //减冻结本金
			$sql="update ld_user_account set freeze_benjin=freeze_benjin-:t1 where id=:t0";
			$sqll="update ld_user_account set freeze_benjin=0 where id=:t0 and freeze_benjin<0";
			$yu=$us["freeze_benjin"]-$jin;
		}
		elseif($arr[2]==25){    //减锁仓冻结金
			$sql="update ld_user_account set freeze_benjin=freeze_benjin-:t1 where id=:t0";
			$sqll="update ld_user_account set freeze_benjin=0 where id=:t0 and freeze_benjin<0";
			$yu=$us["freeze_benjin"]-$jin;
		}elseif($arr[2]==156){
			//增加冻结本金
			$sql="update ld_user_account set btc_account=btc_account-:t1 where id=:t0";
			$sqll="update ld_user_account set btc_account=0 where id=:t0 and btc_account<0";
			$yu=$us["btc_account"]-$jin;
		}elseif($arr[2]==157){
			//增加冻结本金
			$sql="update ld_user_account set ltc_account=ltc_account-:t1 where id=:t0";
			$sqll="update ld_user_account set ltc_account=0 where id=:t0 and ltc_account<0";
			$yu=$us["ltc_account"]-$jin;
		}
		$db->lspre($sql,4,array(array(':t0',':t1'),array($us["id"],$jin)));
		if(!empty($sqll))
		$db->lspre($sqll,4,array(array(':t0'),array($us["id"])));
	    //帮助记录表中添加数据
		$sql="insert into ld_help_record(uid,uname,help_record_time,get_envelope_type,minus_amount,usable_amount,operation_type,description) values(:t1,:t2,:t3,:t4,:t5,:t6,:t7,:t8)";
		$db->lspre($sql,4,array(array(':t1',':t2',':t3',':t4',':t5',':t6',':t7',':t8'),array($us["id"],$us["uname"],$arr[3],$arr[4],$jin,$yu,$arr[2],$arr[6])));
	}
}

function tautoinfo($k,$arr){
	switch ($k) {
		case 'rgmba':
			return $arr['rgmba'];
		break;
		case 'rgmobile':
			return $arr['phone'];
		break;
		case 'rgemail':
			return $arr['rgemail'];
		break;
		case 'rgcard':
			return $arr['rgcard'];
		break;
		case 'rgaddress':
			return $arr['rgaddress'];
		break;
		case 'rgweixin':
			return $arr['rgweixin'];
		break;
		case 'rgqq':
			return $arr['rgqq'];
		break;
		case 'rgsex':
			return getsex($arr['sex']);
		break;
		case 'rgfax':
			return $arr['rgfax'];
		break;
	}
}

function getrand(){
	global $db;
	for($i=0;$i<6;$i++){
		$string.=rand(0,9);
	}
	$string="Y".$string;
	$db->query("select id from ld_user_info where uname='$string'",1);
	if(!empty($db->rs))
	{
		getrand();
	}else{
		return $string;
	}
}

function getregform($r){
	global $GLOBALARR,$db,$config;
	$rr=$config->getregconfig();
	$s='';
	if(is_array($GLOBALARR['jjname'])){
		foreach ($GLOBALARR['jjname'] as $k => $v) {
			$s.='<label><input type="radio" name="ulevel" value="'.$k.'">&nbsp;'.$v.'('.$GLOBALARR['jjrmb'][$k].')</label>&nbsp;&nbsp;';
		}
	}else{
			$s.='<label><input type="radio" name="ulevel" value="0">&nbsp;'.$GLOBALARR['jjname'].'('.$GLOBALARR['jjrmb'].')</label>&nbsp;&nbsp;';
	}
	$arr['ulevel']=$s;

	if(!empty($rr['rgmbq'])){
	$arr['mbq']='<select name="rgmbq" class="'.$r['sclass'].'"><option value="0">请选择您的密保问题?</option>';
	foreach ($rr['rgmbq'] as $k => $v) {
		$arr['mbq'].='<option value="'.($k).'" '.($r['mbq']==($k)?"selected":"").'>'.$v.'</option>';
	}
	$arr['mbq'].='</select>';
	}
	return $arr;
}

function getanpoint($p,$anseat){
	return ($p*3)+$anseat-1;
}

function chkreginfo(&$arr){
	global $member;
	$s='';
	$d=array();
	foreach ($arr as $k => $v) {
		switch ($k) {
			case 'uname':
				if(empty($v)){
					$s='请输入用户名';
					break 2;
				}
				if(!chkusername($v)){
					$s='用户名必须是以字母开头，长度是4-15位的字母，数字和下划线的组合';
					break 2;
				}
				$r=$member->user_exists($v);
				if(!empty($r)){
					$s='用户名已经被使用';
					break 2;
				}
			break;
			case 'introducer_id':
				if(empty($v)){
					$s='请输入推荐人';
					break 2;
				}
				if(!chkusername($v)){
					$s='推荐人输入不正确';
				}
				$r=$member->tu_exists($v);
				if(empty($r)){
					$s='推荐人不存在或者未激活';
					break 2;
				}
				$arr['introducer_id']=$r['id'];
				$arr['parents_ids']=$r['parents_ids'].$r['id'].',';
				$arr['leader_number']=$r['leader_number']+1;

				$d['introducer_id']=$r['id'];
			break;

			case 'user_level':
				if($v===''){
					$s='请选择注册级别';
					break 2;
				}
			break;
		}
	}
	return $s;
}
//获取奖金类型
function getjjtype($s){
	global $GLOBALARR;
	$a='';
	foreach ($GLOBALARR['prizeval'] as $k => $v) {
		if($s==$v){
			$a=$GLOBALARR['prizename'][$k];
			break;
		}
	}
	return $a;
}
//md5(md5('123456yasuima'))
function getpwd($s){
	return md5(md5($s.SITEENCRYPT));
}

//获取获得帮助用户的排队位次
function get_help_order($get_help_time){
	global $db;
	$db->query("select count(*) as theorder from ld_get_help where get_help_time < ".intval($get_help_time)." and status = 0",2);
	return $db->rs['theorder'];
}
//获取提供帮助用户的排队位次
function give_help_order($give_help_time){
	global $db;
	$db->query("select count(*) as theorder from ld_give_help where give_help_time < ".intval($give_help_time)." and status = 0",2);
	return $db->rs['theorder'];
}
//获取提供帮助信息或获得帮助信息
function give_get_help($type,$give_help_id,$get_help_id,$field){
	global $db;
	if($type == 'give'){
		$db->query("select * from ld_give_help where id = ".$give_help_id,2);
		$rs = $db->rs;
		if(!empty($rs)){
			if($field == 'help_no'){
				return $rs['give_help_no'];
			}else{
				return $rs;
			}
		}else{
			return '';
		}
	}elseif($type == 'get'){
		$db->query("select * from ld_get_help where id = ".$get_help_id,2);
		$rs = $db->rs;
		if(!empty($rs)){
			if($field == 'help_no'){
				return $rs['get_help_no'];
			}else{
				return $rs;
			}
		}else{
			return '';
		}
	}else{
		return '';
	}
}
function give_get_helpno($type,$give_help_no,$get_help_no){
	global $db;
	if($type == 'give'){
		$db->query("select * from ld_give_help where give_help_no = '".$give_help_no."'",2);
		$rs = $db->rs;
		if(!empty($rs)){
			return $rs;
		}else{
			return array();
		}
	}elseif($type == 'get'){
		$db->query("select * from ld_get_help where get_help_no = '".$get_help_no."'",2);
		$rs = $db->rs;
		if(!empty($rs)){
			return $rs;
		}else{
			return array();
		}
	}else{
		return array();
	}
}

function notice_all(){
	global $db,$member,$GLOBALARR;
	//
	//查找匹配完成尚未发送过通知的汇款记录发送匹配成功通知
	//先给提供帮助一方发短信    (匹配表中查询未发送短信的  )
	$db->query("select * from ld_remit_list where sms_send = 0 group by give_help_id");
	$give_sms_send = $db->rs;
	$sms_send_arr=array();
	foreach($give_sms_send as $val){
		$db->query("select uid from ld_give_help where id = ".$val['give_help_id'],2);    //帮助表中查找用户的id
		$give_helper_uid = $db->rs['uid'];
		$db->query("select phone from ld_user_info where id = ".$give_helper_uid,2);      //用户信息表中查询数据
		$give_helper_phone = $db->rs['phone'];
		array_push($sms_send_arr,$give_helper_phone);
		$db->sitedbexec("update ld_remit_list set sms_send = 1 where sms_send=0 and give_help_id = ".$val['give_help_id']);//标示状态已发送
	}
	if(!empty($sms_send_arr)){
		$sms_send_arr = array_unique($sms_send_arr); //移除数组中的重复的值，并返回结果数组
		foreach($sms_send_arr as $k=>$v){
			$ress = send_sms($v,"尊敬的会员，您提交的订单已经匹配成功，请及时登录您的平台进行操作，特此提醒！");
		}
	}
	//再给获得帮助一方法短信
	$db->query("select * from ld_remit_list where send_sms = 0 group by get_help_id");
	$get_sms_send = $db->rs;
	$send_sms_arr = array();
	foreach($get_sms_send as $val){
		$db->query("select get_help_uid from ld_get_help where id = ".$val['get_help_id'],2);
		$get_helper_uid = $db->rs['get_help_uid'];
		$db->query("select phone from ld_user_info where id = ".$get_helper_uid,2);
		$get_helper_phone = $db->rs['phone'];
		array_push($send_sms_arr,$get_helper_phone);
		$db->sitedbexec("update ld_remit_list set send_sms = 1 where send_sms = 0 and get_help_id = ".$val['get_help_id']);
	}
	if(!empty($send_sms_arr)){
		$send_sms_arr = array_unique($send_sms_arr);
		foreach($send_sms_arr as $key=>$val){
			$ress = send_sms($val,"尊敬的会员，您提交的订单已经匹配成功，请及时登录您的平台进行操作，特此提醒！");
		}
	}
	return true;

}
function xmltoarray($xml){
	$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
	if(preg_match_all($reg, $xml, $matches)){
		$count = count($matches[0]);
		for($i = 0; $i < $count; $i++){
			$subxml= $matches[2][$i];
			$key = $matches[1][$i];
			if(preg_match( $reg, $subxml )){
				$arr[$key] = xmltoarray( $subxml );
			}else{
				$arr[$key] = $subxml;
			}
		}
	}
	return $arr;
}
//发送手机短信
function send_sms($mobile,$message){
	/*
	$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	$post_data = "account=&password=&mobile=".$mobile."&content=".rawurlencode($message);
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $target);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_NOBODY, true);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
	$return_str = curl_exec($curl);
	curl_close($curl);
	$gets = xmltoarray($return_str);
	if($gets['SubmitResult']['code']==2){
		return true;
	}else{
		return false;
	}
	return false;*/
}
//根据汇款id获取消息数目
function getnoticenum($remit_id,$uid){
	global $db;
	$db->query("select count(0) as num from ld_chat where remit_id = ".$remit_id." and uid != ".$uid,2);
	return $db->rs['num'];
}
//根据id取得提供帮助编号
function get_order_no($help_id){
	global $db;
	$db->query("select give_help_no from ld_give_help where id = ".$help_id,2);
	return $db->rs['give_help_no'];
}
//根据id取得获得帮助编号
function get_order_num($help_id){
	global $db;
	$db->query("select get_help_no from ld_get_help where id = ".$help_id,2);
	return $db->rs['get_help_no'];
}
//检查目前是否可以自动匹配，如果可以返回true，否则返回false
function autopdopen($thetime){
	global $GLOBALARR;

	if($GLOBALARR['autoorno'] != 0 || $GLOBALARR['syopen'] != 0){
		return false;
	}else{
		$startstr1 = date("Y-m-d")." ".$GLOBALARR['morningstart'].":00";
		$starttime1 = strtotime($startstr1);
		$endstr1 = date("Y-m-d")." ".$GLOBALARR['morningend'].":00";
		$endtime1 = strtotime($endstr1);
		$startstr2 = date("Y-m-d")." ".$GLOBALARR['noonstart'].":00";
		$starttime2 = strtotime($startstr2);
		$endstr2 = date("Y-m-d")." ".$GLOBALARR['noonend'].":00";
		$endtime2 = strtotime($endstr2);
		if($thetime>=$starttime1 && $thetime<=$endtime1){
			return true;
		}else{
			if($thetime>=$starttime2 && $thetime<=$endtime2){
				return true;
			}else{
				return false;
			}
		}
	}
}

//获取用户最近一次挂单的金额
function get_latest_give($uid){
	global $db;

	$db->query("select amount from ld_give_help where `status`<3 and uid = ".$uid."  and  xf_type=2 order by give_help_time desc limit 1",2);
	$help_common = $db->rs;
	if(!empty($help_common)){
		return intval($help_common['amount']);
	}else{
		return 0;
	}
}
function is_duibi($uid){
	global $db;
	//是否提现了
	$db->query("select id from ld_transfer_coin where type=0 and ctype=0 and is_cross=1 and cuid=".$uid,2);
	if($db->rs['id']){
		return 0;
	}else{
		return 1;
	}


}
//获取用户上次提供帮助距离现在的时间，返回秒数。如果没有提供过帮助直接返回当前时间的时间戳
function get_latest_givetime($uid){
	global $db;
	$db->query("select give_help_time from ld_give_help where `status`<3 and uid = ".$uid." order by give_help_time desc limit 0,1",2);
	$give_help_time=$db->rs['give_help_time'];
	if(!empty($give_help_time)){
		$cha = time()-$give_help_time;
		return $cha;
	}else{
		return time();
	}
}
function get_is_pai($uid){
	global $db,$GLOBALARR;
	$db->query("select give_help_time from ld_give_help where `status`=0 and uid = ".$uid."  and ".time()."-give_help_time<".$GLOBALARR['perdays']."*24*3600  order by give_help_time desc limit 0,1",2);
	$give_help_time=$db->rs['give_help_time'];
	if(!empty($give_help_time)){
		return 1;
	}else{
		return 0;
	}
}
//正在拍单的金额
function get_pai_amount($uid){
	global $db,$GLOBALARR;
	$db->query("select amount from ld_give_help where `status`=0 and uid = ".$uid."  and ".time()."-give_help_time<".$GLOBALARR['perdays']."*24*3600 and xf_type=2 order by give_help_time desc limit 0,1",2);
	$amount=$db->rs['amount'];
	if(!empty($amount)){
		return $amount;
	}else{
		return 0;
	}
}
function get_dz_amount($uid){
	global $db,$GLOBALARR;
	$db->query("select amount from ld_give_help where  uid = ".$uid."  and xf_type=1 order by give_help_time desc limit 0,1",2);
	$amount=$db->rs['amount'];
	if(!empty($amount)){
		return $amount;
	}else{
		return 0;
	}
}
//取得用户最近一次完成提供帮助金额，如果没有完成过返回0
function get_finished_give($uid){
	global $db;
	$db->query("select * from ld_give_help where giveh_eviup_time>0 and uid = ".$uid." and helper_confirm_amount=amount order by giveh_eviup_time desc limit 0,1",2);
	$give_help = $db->rs;
	if(!empty($give_help)){
		return $give_help['amount'];
	}else{
		return 0;
	}
}
//查询是否超过一天的提供帮助金额上限
function out_of_help($jine){
	global $db,$GLOBALARR;
	$start_time = strtotime(date("Y-m-d")." 00:00:00");
	$end_time = strtotime(date("Y-m-d")." 23:59:59");
	$db->query("select sum(amount) as samount from ld_give_help where give_help_time>=".$start_time." and give_help_time<=".$end_time." and `status`<3 and amount > 0",2);
	$today_amount = $db->rs['samount'];
	if($jine+$today_amount > $GLOBALARR['qudelay']){
		return true;
	}else{
		return false;
	}
}
//计算用户可以获取收益的提供帮助总金额
function get_stop_benifit($uname){
	global $db,$GLOBALARR;
	$res_amount=0;
	$db->query("select * from ld_give_help where had_shouyi=0 and benifit_times >= ".$GLOBALARR["jjsyt"]." and amount = helper_confirm_amount and giveh_eviup_time > 0 and (TIMESTAMPDIFF(hour , from_unixtime(giveh_eviup_time,'%Y-%m-%d %H:%i:%S'),  '".date("Y-m-d H:i:s")."')>=".$GLOBALARR['freezedhour'].") and uname='".$uname."'");
	$rs=$db->rs;
	if(!empty($rs)){
		foreach($rs as $k=>$v){
			if(not_full_benifit($v['id'])){
				$res_amount = $res_amount+$v["benifit_amount"]*0.5+$v["amount"];
			}else{
				$res_amount = $res_amount+$v["benifit_amount"]+$v["amount"];
			}
		}
	}
	return $res_amount;
}
function not_full_benifit($give_help_id){
	global $db;
	$db->query("select * from ld_remit_list where evidence_upload_time-deal_time>21600 and give_help_id=".$give_help_id,2);
	$rs=$db->rs;
	if(!empty($rs)){
		return true;
	}else{
		$db->query("select * from ld_remit_list where evidence_upload_time is null and ".time()."-deal_time>21600 and give_help_id=".$give_help_id,2);
		$rss=$db->rs;
		if(!empty($rss)){
			return true;
		}
	}
	return false;
}
//获取用户历史挂单总额
function get_sum_give($uid){
	global $db;
	$db->query("select sum(amount) as samount from ld_give_help where status<3 and uid=".$uid,2);
	$give_help_amount=$db->rs['samount'];
	return intval($give_help_amount);
}
//获取提供帮助总次数
function give_help_ci($uid){
	global $db;
	$db->query("select count(0) as ci from ld_give_help where status<3 and uid=".$uid,2);
	$give_help_ci=$db->rs['ci'];
	if(empty($give_help_ci)){
		$give_help_ci=0;
	}
	return $give_help_ci;
}
//每种钱包一天只能提现一次
function can_withdraw($uid,$benxi,$ldprize){
	global $db;
	$start_time=strtotime(date("Y-m-d"));
	$end_time=strtotime(date("Y-m-d 23:59:59"));
	if(!empty($benxi)){
		$db->query("select * from ld_get_help where status<3 and get_help_uid=".$uid." and cash_bx>0 and get_help_time>=".$start_time." and get_help_time<=".$end_time,2);
		$get_help_info=$db->rs;
		if(!empty($get_help_info)){
			return array('status'=>0,'mes'=>'一天只能提一次本息！');
		}
	}
	if(!empty($ldprize)){
		$db->query("select * from ld_get_help where status<3 and get_help_uid=".$uid." and cash_ld>0 and get_help_time>=".$start_time." and get_help_time<=".$end_time,2);
		$get_help_info=$db->rs;
		if(!empty($get_help_info)){
			return array('status'=>0,'mes'=>'一天只能提一次领导奖！');
		}
	}
	return array('status'=>1);
}
//10天之后自动发放利息
/*
 *8天一排单10天一利息
 */
function auto_falixi(){
	global $db,$GLOBALARR;

	try{  //
		$db->begintransaction();
		//收益完成还未发放利息
	    $db->query("select * from ld_give_help where status=0 and status_value=1 order by id asc");
        $list=$db->rs;
        foreach ($list as $key => $v) {
        	$status=0;
        	$stime=time()-$GLOBALARR['perdays']*24*3600;
            $fa=0;
        	if($v['give_help_time']<$stime){
        	       $fa=1;
        	}
        	if($v['xf_type']==1){
        		$xftype='商品积分';

        	}else{
        		$xftype='赠送积分';

        	}
            if($fa==1){
            	$bjin=$v['amount'];
        	    $lixi=$v['benifit_amount'];
        	    $tot=$bjin+$lixi;
        	    $newlixi=$lixi*0.9;
        	    $fxjifen=$lixi*0.1;
        	    $db->lspre("update ld_user_account set total_prize=total_prize+:t1 where id=:t0",4,array(array(':t0',':t1'),array($v['uid'],$tot)));
			    //增加利息
		        writeaccount(array($v['uid'],"0",11,time(),31,$newlixi,'利息'.$lixi.'的90%转入静态收益-排单编号|'.$v['give_help_no']));
			    //增加共享积分
			    writeaccount(array($v['uid'],"0",20,time(),201,$bjin,$xftype.'收益转入共享积分-编号|'.$v['give_help_no']));
			     //增加积分
			    writeaccount(array($v['uid'],"0",12,time(),35,$fxjifen,'利息'.$lixi.'的10%转入商城积分钱包-排单编号|'.$v['give_help_no']));

			    //状态更新
        	    $db->query("update ld_give_help set status=1,flx_time=".time().",had_shouyi=1 where id=".$v['id'],4);
        	    //prize_list 表也更新
        	    $db->sitedbexec("update ld_prize_list set prize_status=1 where help_id=".$v['id']." and prize_option=51 and prize_status=0  and uid=".$v['uid']."");

            }

        }
	    $db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}

}

function get_lever($id){
	global $db;
	$db->query("select user_level from ld_user_account where id=".$id,2);
	$lever=$db->rs['user_level'];
	return $lever;
}
//10天之后自动发放领导奖
function auto_faldj(){
	global $db,$GLOBALARR;
	try{  //
		$db->begintransaction();
	    $db->query("select ld_prize_list.id as id,ld_prize_list.amount as amount,ld_prize_list.uid as uid,lx_generation,ld_give_help.give_help_no as give_help_no from ld_prize_list left join ld_give_help on help_id=ld_give_help.id  where prize_option=52  and prize_status=0 ");
        $list=$db->rs;

        foreach ($list as $key => $v) {

            	$ldjin=$v['amount']*$GLOBALARR['duib_dt']/100;
        	    $jifen=$v['amount']*$GLOBALARR['duifx_dt']/100;
        	    $fazhan=$v['amount']*$GLOBALARR['duifz_dt']/100;
			    //增加领导奖
		        writeaccount(array($v['uid'],"0",9,time(),33,$ldjin,'领导奖'.$v['amount'].'的'.$GLOBALARR['duib_dt'].'%转入动态钱包-排单编号|'.$v['give_help_no']));
			    //增加商城积分
			    writeaccount(array($v['uid'],"0",12,time(),35,$jifen,'领导奖'.$v['amount'].'的'.$GLOBALARR['duifx_dt'].'%转入商城积分钱包-排单编号|'.$v['give_help_no']));
			     //增加发展基金7/25
			    writeaccount(array($v['uid'],"0",22,time(),220,$fazhan,'领导奖'.$v['amount'].'的'.$GLOBALARR['duifz_dt'].'%转入发展基金-排单编号|'.$v['give_help_no']));
			    //状态更新
        	    $db->query("update ld_prize_list set prize_status=1 where id=".$v['id'],4);

        }


	    $db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
}
function auto_fachanel(){
	global $db,$GLOBALARR;
	try{  //
		$db->begintransaction();
	    $db->query("select ld_prize_list.id as id,ld_prize_list.amount as amount,ld_prize_list.uid as uid,lx_generation,ld_give_help.give_help_no as give_help_no,ld_give_help.status as status from ld_prize_list left join ld_give_help on help_id=ld_give_help.id  where prize_option=55 and prize_status=0 ");
        $list=$db->rs;
        if(!$list){
        	return;
        }
        foreach ($list as $key => $v) {
        	    if($v['amount']>0){
			        writeaccount(array($v['uid'],"0",9,time(),33,$v['amount'],'渠道奖'.$v['amount'].'转入动态钱包-排单编号|'.$v['give_help_no']));
				    //状态更新
        	    }
        	    $db->query("update ld_prize_list set prize_status=1 where id=".$v['id'],4);

        }


	    $db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
}
//自动更新攒币价格
function updateprice(){
	global $db;
	$db->query("select * from ld_coin_price order by update_time desc limit 1",2);
	$res=$db->rs;
	$jiekou=file_get_contents("https://www.dcpplus.com/real/price.html?symbol=18 ");
	$biarr=json_decode($jiekou,true);
	var_dump($biarr);
	if($biarr['p_new']!=0){
	if($res['price']!=$biarr['p_new']){
		$now_price=$biarr['p_new'];
		$sql="insert into ld_coin_price values('',:t0,:t1)";
		$db->lspre($sql,4,array(array(':t0',':t1'),array($now_price,time())));
	}
	}
}
//自动更新btcusdt价格
function updateprice_btc(){
	global $db;
	$db->query("select * from ld_btc_price order by update_time desc limit 1",2);
	$res=$db->rs;
	$jiekou=file_get_contents("https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol=btcusdt");
	$biarr=json_decode($jiekou,true);
	var_dump($biarr["data"][0]['close']);

	if(round($biarr["data"][0]['close'],2)!=0){
	if(round($res['price'],2)!=round($biarr["data"][0]['close'],2)){
		$now_price=round($biarr["data"][0]['close'],2);
		$sql="insert into ld_btc_price values('',:t0,:t1)";
		$db->lspre($sql,4,array(array(':t0',':t1'),array($now_price,time())));
	}
	}
}
//自动更新ltcusdt价格
function updateprice_ltc(){
	global $db;
	$db->query("select * from ld_ltc_price order by update_time desc limit 1",2);
	$res=$db->rs;
	$jiekou=file_get_contents("https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol=ltcusdt");
	$biarr=json_decode($jiekou,true);
	var_dump($biarr["data"][0]['close']);

	if(round($biarr["data"][0]['close'],2)!=0){
	if(round($res['price'],2)!=round($biarr["data"][0]['close'],2)){
		$now_price=round($biarr["data"][0]['close'],2);
		$sql="insert into ld_ltc_price values('',:t0,:t1)";
		$db->lspre($sql,4,array(array(':t0',':t1'),array($now_price,time())));
	}
	}
}
//自动更新人民币的汇率
function updateprice_hv(){
	global $db;
	$db->query("select * from ld_rate order by update_time desc limit 1",2);
	$ress=$db->rs;

	$ch = curl_init();
    $url = 'http://apis.baidu.com/netpopo/exchange/convert?from=USD&to=CNY&amount=10';
    $header = array(
        'apikey: dadcdd40cb94dc393e16a4535b61beda',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    $res = curl_exec($ch);
    $biarr=json_decode($res,true);
    var_dump(($biarr));

	if($biarr["result"]["rate"]!=0){
	if($ress['rate']!=$biarr["result"]["rate"]){
		$now_price=$biarr["result"]["rate"];
		$sql="insert into ld_rate values('',:t0,:t1)";
		$db->lspre($sql,4,array(array(':t0',':t1'),array($now_price,time())));
	}
	}
}

//获取当前
function getprice(){
	global $db;
	$db->query("select * from ld_coin_price order by update_time desc limit 1",2);
	$now_price=$db->rs;
	return $now_price['price'];
}
function getprice_btc(){
	global $db;
	$db->query("select * from ld_btc_price order by update_time desc limit 1",2);
	$now_price=$db->rs;
	return $now_price['price'];
}
function getprice_ltc(){
	global $db;
	$db->query("select * from ld_ltc_price order by update_time desc limit 1",2);
	$now_price=$db->rs;
	return $now_price['price'];
}
function getprice_rate(){
	global $db;
	$db->query("select * from ld_rate order by update_time desc limit 1",2);
	$now_price=$db->rs;
	return $now_price['rate'];
}
//今天兑换数字加密资产的总金额
function todaydh(){
	global $db,$_userid,$GLOBALARR;
	$stime=strtotime(date("Y-m-d 00:00:00"));
	$etime=strtotime(date("Y-m-d 23:59:59"));
	$db->query("select sum(ctotal) as tot from ld_transfer_coin where cuid=".$_userid." and ctime between ".$stime." and ".$etime." and type=0",2);
	$tnum=$db->rs['tot'];
	if(empty($tnum)){
		$tnum=0;
	}
	return $tnum;
}
//10天之后自动发放晋级奖
function auto_fajinji(){
	global $db;

	try{  //
		$db->begintransaction();
	    $db->query("select ld_prize_list.id as id,ld_prize_list.amount as amount,ld_prize_list.uid as uid,lx_generation,ld_give_help.give_help_no as give_help_no,ld_give_help.status as status from ld_prize_list left join ld_give_help on help_id=ld_give_help.id  where prize_option=62  and prize_status=0 ");
        $list=$db->rs;

        foreach ($list as $key => $v) {
            $jjamount=$v['amount'];
            writeaccount(array($v['uid'],"0",13,time(),37,$jjamount,'晋级奖'.$v['amount'].'转入晋级钱包-排单编号|'.$v['give_help_no']));
            $db->query("update ld_prize_list set prize_status=1 where id=".$v['id'],4);

        }


	    $db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
}
//获取用户兑换美业币的记录
function duihuan_record($type,$uid,$start,$end,$act,$order=" ctime desc",$pagecount=12){
	global $db;
	$condition=" where id>0 and ctype<25";
	if(!empty($type)){
		$condition .= " and type=".$type;
	}else{
		$condition .= " and type=0";
	}
	if(!empty($uid)){
		$condition .= " and cuid=".$uid;
	}
	if(!empty($start)){
		$start_time=strtotime($start);
		$condition .= " and ctime>=".$start_time;
	}
	if(!empty($end)){
		$end_time=strtotime($end);
		$condition .= " and ctime<=".$end_time;
	}
	$psql="select count(0) from ld_transfer_coin $condition";
	$db->query($psql,3);
	$pcount=$db->fval;
	$astr="&lsact=".$act."&templist=1";
	$pagearr=pagelist($pagecount,$pcount,$astr);
	$offset=$pagearr[1];
	$sql="select * from ld_transfer_coin $condition order by $order limit $offset,$pagecount";
	$db->query($sql);
	$rs=$db->rs;
	// return $condition;
	return array('arr'=>$rs,'pagearr'=>$pagearr,'pcount'=>$pcount,'pagecount'=>$pagecount);
}
//获取用户兑换美业币的记录
function duihuanpbc_record($type,$uid,$start,$end,$act,$order=" ctime desc",$pagecount=12){
	global $db;
	$condition=" where id>0 and ctype<25";
	if(!empty($type)){
		$condition .= " and type=".$type;
	}else{
		$condition .= " and type=0";
	}
	if(!empty($uid)){
		$condition .= " and cuid=".$uid;
	}
	if(!empty($start)){
		$start_time=strtotime($start);
		$condition .= " and ctime>=".$start_time;
	}
	if(!empty($end)){
		$end_time=strtotime($end);
		$condition .= " and ctime<=".$end_time;
	}
	$psql="select count(0) from ld_zhuanhuan_log $condition";
	$db->query($psql,3);
	$pcount=$db->fval;
	$astr="&lsact=".$act."&templist=1";
	$pagearr=pagelist($pagecount,$pcount,$astr);
	$offset=$pagearr[1];
	$sql="select * from ld_zhuanhuan_log $condition order by $order limit $offset,$pagecount";
	$db->query($sql);
	$rs=$db->rs;
	// return $condition;
	return array('arr'=>$rs,'pagearr'=>$pagearr,'pcount'=>$pcount,'pagecount'=>$pagecount);
}
function qianbao_delete($uid,$id){
	global $db;
		$ddddd=$db->sitedbexec('delete from ld_user_qianbao where id=' . $id.' and cuid='.$uid);
		return $ddddd;
	}
function qianbao_record($type,$uid,$start,$end,$act,$order=" ctime desc",$pagecount=12){
	global $db;
	$condition=" where id>0 ";
	// if(!empty($type)){
	// 	$condition .= " and type=".$type;
	// }else{
	// 	$condition .= " and type=0";
	// }
	if(!empty($uid)){
		$condition .= " and cuid=".$uid;
	}
	if(!empty($start)){
		$start_time=strtotime($start);
		$condition .= " and ctime>=".$start_time;
	}
	if(!empty($end)){
		$end_time=strtotime($end);
		$condition .= " and ctime<=".$end_time;
	}
	$psql="select count(0) from ld_user_qianbao $condition";
	$db->query($psql,3);
	$pcount=$db->fval;
	$astr="&lsact=".$act."&templist=1";
	$pagearr=pagelist($pagecount,$pcount,$astr);
	$offset=$pagearr[1];
	$sql="select * from ld_user_qianbao $condition order by $order limit $offset,$pagecount";
	$db->query($sql);
	$rs=$db->rs;
	// return $condition;
	return array('arr'=>$rs,'pagearr'=>$pagearr,'pcount'=>$pcount,'pagecount'=>$pagecount);
}
function qianbao_list($uid,$coin){
	global $db;
	$condition=" where id>0 and cuid=".$uid." and coin='".$coin."'";
	$sql="select * from ld_user_qianbao $condition ";
	// return $sql;
	$db->query($sql);
	$rs=$db->rs;
	// return $sql;
	return array('arr'=>$rs);
}
function toUserCoin_list($uid){
    global $db;
    $condition=" where id>0 and fromUserID=".$uid;
    $sql="select * from ld_toUserCoin $condition ";
    // return $sql;
    $db->query($sql);
    $rs=$db->rs;
    // return $sql;
    return array('arr'=>$rs);
}
function get_address($id){
	global $db;
	$db->query("select * from ld_address where adid=".$id." order by is_default desc");
	$list=$db->rs;
	if($list){
		return $list[0];

	}else{
		return;

	}


}
function getamount($id){
	global $db;
	$db->query("select sum(amount) as num from ld_give_help  where status=0 and is_yuyue=0 and  uid=".$id." ",2);
	$amount=$db->rs['num'];
	if(empty($amount)){
		$amount=0;
	}
	return $amount;
}
function getyyamount($id){
	global $db;
	$db->query("select sum(amount) as num from ld_give_help  where status=0 and is_yuyue=1 and uid=".$id." ",2);
	$amount=$db->rs['num'];
	if(empty($amount)){
		$amount=0;
	}
	return $amount;
}
//最后一次消费的金额
function get_last_help($id){
	global $db;
	$db->query("select amount from ld_give_help where uid=".$id." order by id desc limit 1",2);
	$amount=$db->rs['amount'];
	return $amount;
}
function get_province(){
	global $db;
	$db->query("select * from ld_province order by id asc");
	$list=$db->rs;
	return $list;
}
//防止重复提交
function check_ipop_limit($module,$time_span=3)
{
	$op = $_SESSION[$module.'check'];
	if(empty($op))
	{
		$check['ip'] = get_client_ip();
		$check['time'] = get_gmtime();
		$_SESSION[$module.'check']=$check;
		return true;  //不存在session时验证通过
	}
	else
	{
		$check['ip'] = get_client_ip();
		$check['time'] = get_gmtime();
		$origin	= $_SESSION[$module.'check'];

		if($check['ip']==$origin['ip'])
		{
			if($check['time'] - $origin['time'] < $time_span)
			{
				return false;
			}
			else
			{
				$_SESSION[$module.'check']=$check;
				return true;  //不存在session时验证通过
			}
		}
		else
		{
			$_SESSION[$module.'check']=$check;
			return true;  //不存在session时验证通过
		}
	}
}
function get_client_ip(){
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
       $ip = getenv("HTTP_CLIENT_IP");
   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
       $ip = getenv("HTTP_X_FORWARDED_FOR");
   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
       $ip = getenv("REMOTE_ADDR");
   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
       $ip = $_SERVER['REMOTE_ADDR'];
   else
       $ip = "unknown";
   return($ip);
}
function get_gmtime()
{
	return (time() - date('Z'));
}
function has_out($uid){
	global $db;
	$db->query("select * from ld_out_rule where out_type=0 and out_uid=".$uid." order by id asc limit 1",2);
    $ruinfo=$db->rs;
    return $ruinfo;
}
function yujitot($amount,$ctype,$cdays){
	global $db,$GLOBALARR;
	$taocanday = array(
        "A" => 0,
        "B" => 1,
        "C" => 2,
        "D" => 3,
        "E" => 4
    );
    $ukey = $taocanday[$ctype];
	$tot= $GLOBALARR['allowtxall'][$ukey] / 100 * $amount*$cdays;
	return sprintf("%.4f",$tot);


}





?>

